﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin.BlogManager
{
    public partial class BlogDetail : BasePage
    {
        private string id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"] + "";

            if (!Page.IsPostBack)
            {
                LoadContent();
                LoadReplyContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }

        #region 加载列表
        private void LoadReplyContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            ReplyHelper rhelper = new ReplyHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "ObjectID", id);
            c.Add(CriteriaType.Equals, "ObjectType", 1);

            PageConfig(ReplyUPager);

            ReplyUPager.ItemCount = rhelper.QueryReplyCountBySearch(c);

            // 以下的值从BasePage中得到
            ReplyUPager.UrlFormat = UrlFormat;
            ReplyUPager.PrefixText = GetPreFixText(ReplyUPager);

            if (mark)
            {
                PagerInit(ReplyUPager);
            }

            string[] fields = new string[] { "ID", "ObjectID", "ObjectType", "UsersID", "ReplyContent","Flag", "CreateDate" };

            DataTable dt = new DataTable();
            dt = rhelper.QueryReplyBySearchToTable(c, new Order[] { new Order("CreateDate") }, ReplyUPager.Begin - 1, ReplyUPager.Count, fields);


            dt.Columns.Add(new DataColumn("No"));
            dt.Columns.Add(new DataColumn("ReplyNickName"));
            dt.Columns.Add(new DataColumn("strCreateDate"));

            int index = 1;
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    dr["No"] = index.ToString();

                    string userid = dr["UsersID"] + "";
                    Users uinfo = new Users();
                    UsersHelper uhelper = new UsersHelper();
                    uinfo = uhelper.GetUsers(userid);

                    string nickname = uinfo.NickName;

                    dr["ReplyNickName"] = nickname;

                    DateTime dtCreateDate = Convert.ToDateTime(dr["createdate"]);
                    dr["strCreateDate"] = dtCreateDate.ToString("yyyy-MM-dd");

                    index++;
                }
            }

            rptReply.DataSource = dt;
            rptReply.DataBind();
        }
        #endregion

        #region 列表事件
        protected void rptReply_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");

            string replyid = hidID.Value;
            if (e.CommandName == "delete")//删除
            {
                
                Reply rinfo = new Reply();
                ReplyHelper rhelper = new ReplyHelper();

                rinfo = rhelper.GetReply(replyid);
                rinfo.ID = replyid;
                rinfo.Flag = 9;
                rhelper.EditReply(rinfo);

                //删除回复时更改blog表中的replycount字段
                Blog binfo = new Blog();
                BlogHelper bhelper = new BlogHelper();
                binfo = bhelper.GetBlog(id);
                int replycount = binfo.ReplyCount;

                int newreplycount = replycount - 1;
                binfo.ID = id;
                bhelper.EditBlog(binfo);

                Tools.Alert("删除成功！");
            }

            LoadReplyContent(false);
        }
        #endregion

        #region 加载内容
        private void LoadContent()
        {
            Blog binfo = new Blog();
            BlogHelper bhelper = new BlogHelper();

            binfo = bhelper.GetBlog(id);
            string blogtypeid = binfo.BlogTypeID;
            //类别
            BlogType btinfo = new BlogType();
            BlogTypeHelper bthelper = new BlogTypeHelper();
            btinfo = bthelper.GetBlogType(blogtypeid);
            litBlogType.Text = btinfo.BlogTypeName;

            string userid = binfo.UsersID;
            //发表人
            Users uinfo = new Users();
            UsersHelper uhelper = new UsersHelper();
            uinfo = uhelper.GetUsers(userid);
            litNickName.Text = uinfo.NickName;

            litPVCount.Text = binfo.PVCount.ToString();
            litReplyCount.Text = binfo.ReplyCount.ToString();
            litRecommandCount.Text = binfo.RecommandCount.ToString();
            litCreateDate.Text = binfo.CreateDate.ToString("yyyy-MM-dd");
            litTitle.Text = binfo.Title;
            lblContent.Text = binfo.BlogContent;
        }
        #endregion

        #region 返回
        protected void ReturnBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("BlogList.aspx");
        }
        #endregion

        #region 推荐
        protected void btnRecommand_Click(object sender, EventArgs e)
        {
            Blog binfo = new Blog();
            BlogHelper bhelper = new BlogHelper();
            binfo = bhelper.GetBlog(id);
            binfo.IsRecommand = 1;
            bhelper.EditBlog(binfo);

            Tools.AlertAndRedirect("推荐成功", "BlogList.aspx");
        }
        #endregion

        #region 屏蔽
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Blog binfo = new Blog();
            BlogHelper bhelper = new BlogHelper();
            binfo = bhelper.GetBlog(id);
            binfo.Flag = -9;
            bhelper.EditBlog(binfo);

            Tools.AlertAndRedirect("屏蔽成功", "BlogList.aspx");
        }
        #endregion
    }
}